<?php

/**
 * 栏目页
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: ListController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Home\Controller;
class ListController extends CommonController{
    private $TypeModel;
    private $ContentModel;
    public function _initialize() {
        parent::_initialize();
        $this->TypeModel = D("Contype");
        $this->ContentModel = D("Content");
        $typeid = I("get.tid", 0, "intval");
        if($typeid <= 0){
            $this->error("对不起，你输入的参数有误！");
        }
        $this->TypeId = $typeid;
        $this->ContentModel->TypeInfos = $this->TypeInfos = $this->TypeModel->getTypeInfos($typeid);
        if(!isArrayAndNotEmpty($this->TypeInfos)){
            $this->error("对不起，你输入的参数有误！", "javascript:;");
        }
        $data = array();
        //当前位置
        $data['position'] = $this->GetPositionLink(true);
        //网页标题
        $data['title'] = preg_replace("/[<>]/", " / ", htmlspecialchars_decode($this->GetPositionLink(false)));
        //栏目名称
        $data['typename'] = $this->TypeInfos['typename'];
        //栏目ID
        $data['typeid'] = $this->TypeInfos['id'];
        //上级栏目ID
        $data['reid'] = $this->TypeInfos['reid'];
        //顶级栏目ID
        $data['topid'] = $this->TypeInfos['topid'];
        //当前栏目链接
        $data['typeurl'] = $this->TypeInfos['typeurl'];
        //栏目内容
        $data['content'] = htmlspecialchars_decode($this->TypeInfos['content']);
        //SEO标题
        $data['seotitle'] = $this->TypeInfos['seotitle'];
        
        //栏目关键字没有就用网站默认关键字
        if($this->TypeInfos['keywords'] != ""){
            $data['keywords'] = $this->TypeInfos['keywords'];
        }
        //栏目描述没有就用网站默认描述
        if($this->TypeInfos['description'] != ""){
            $data['description'] = $this->TypeInfos['description'];
        }
        $this->assign($data);
        $this->assign("field", $this->TypeInfos);
    }

    public function index() {
        if($this->TypeInfos['ispart'] > 0)
        {
            $this->DisplayPartTemplets();
            return ;
        }else{
            $this->DisplayListTemplets();
        }
    }
    
    /**
     *  显示单独模板页面
     *
     * @access    public
     * @param     string
     * @return    string
     */
    private function DisplayPartTemplets(){
        if($this->TypeInfos['ispart']==1){
            //封面模板
            $tempfile = T($this->TypeInfos['tempindex']);
            if(!file_exists($tempfile)){
                $tempfile = T("index_default");
            }
            $this->display($tempfile);
        }else if($this->TypeInfos['ispart'] == 2){
            //跳转网址
            $gotourl = $this->TypeInfos['typedir'];
            redirect($gotourl);
            exit();
        }
    }
    
    /**
     * 显示列表模板页面
     */
    private function DisplayListTemplets(){
        $tempfile = T($this->TypeInfos['templist']);
        if(!file_exists($tempfile)){
            $tempfile = T("list_default");
        }
        //var_dump($this->TypeInfos);die;
        $sonIds = $this->TypeModel->getSonId($this->TypeId, $this->TypeInfos['model_id']);
        $where = array("typeid" => array("in", $sonIds));
        $result = $this->getPagination("Content", $where);
        $this->assign("list", $result['data']);
        $this->assign("page", $result['page']);
        $this->assign("count", $result['total_rows']);
        
        //var_dump($result['data']);die;
        
        $this->display($tempfile);
    }
}
